package smarthouse.conv;

import java.util.ArrayList;
import java.util.List;

import javax.ejb.Local;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import smarthouse.dto.ElectronicDTO;
import smarthouse.dto.HouseDTO;
import smarthouse.dto.PrivilegeDTO;
import smarthouse.dto.RoomDTO;
import smarthouse.dto.UserDTO;
import smarthouse.model.Electronic;
import smarthouse.model.House;
import smarthouse.model.Privilege;
import smarthouse.model.Room;
import smarthouse.model.User;

/**
 * Session Bean implementation class Conversions
 */
/**
 * @author Sergiu
 * 
 */
@Stateless
@Local
public class Conversions {

	@PersistenceContext
	EntityManager em;

	/**
	 * Default constructor.
	 */
	public Conversions() {

	}

	/**
	 * Convert class to DTO
	 * 
	 * @param user
	 * @return
	 */
	public UserDTO user_C_2_DTO(User user) {
		UserDTO userDTO = new UserDTO();
		userDTO.setUserId(user.getUserId());
		userDTO.setHouse(user.getHouse().getHouseId());
		userDTO.setName(user.getName());
		userDTO.setPassword(user.getPassword());
		userDTO.setUsername(user.getUsername());
		userDTO.setUserType(user.getUserType());
		return userDTO;
	}

	/**
	 * Convert class to DTO
	 * 
	 * @param elec
	 * @return
	 */
	public ElectronicDTO electronic_C_2_DTO(Electronic elec) {
		ElectronicDTO elecDTO = new ElectronicDTO();

		elecDTO.setElectronicId(elec.getElectronicId());
		elecDTO.setPower(elec.getPower());
		elecDTO.setType(elec.getType());

		return elecDTO;
	}

	/**
	 * Convert class to DTO
	 * 
	 * @param h
	 * @return
	 */
	public HouseDTO house_C_2_DTO(House h) {
		HouseDTO hDTO = new HouseDTO();
		hDTO.setHouseId(h.getHouseId());
		hDTO.setAddress(h.getAddress());
		hDTO.setAdminPassword(h.getAdminPassword());
		hDTO.setHouseName(h.getHouseName());
		if (h.getHousePicture() != null) {
			hDTO.setHousePicture(h.getHousePicture());
		}
		hDTO.setLatitude(h.getLatitude());
		hDTO.setLongitude(h.getLongitude());
		hDTO.setPostalCode(h.getPostalCode());

		for (User u : h.getUsers()) {
			hDTO.getUsers().add(u.getUserId());
		}

		for (Room r : h.getRooms()) {
			hDTO.getRooms().add(r.getRoomId());
		}

		return hDTO;
	}

	/**
	 * Convert class to DTO
	 * 
	 * @param priv
	 * @return
	 */
	public PrivilegeDTO privilege_C_2_DTO(Privilege priv) {
		PrivilegeDTO privDTO = new PrivilegeDTO();

		privDTO.setHouseLook(priv.getHouseLook());
		privDTO.setLights(priv.getLights());
		privDTO.setRoomEl(priv.getRoomEl());
		privDTO.setTemp(priv.getTemp());
		privDTO.setUserId(priv.getUserId());

		return privDTO;
	}

	/**
	 * Convert class to DTO
	 * 
	 * @param room
	 * @return
	 */
	public RoomDTO room_C_2_DTO(Room room) {

		RoomDTO roomDTO = new RoomDTO();

		roomDTO.setLight(room.getLight());
		roomDTO.setRoomId(room.getRoomId());
		roomDTO.setSize(room.getSize());
		roomDTO.setTemperature(room.getTemperature());
		roomDTO.setType(room.getType());

		for (Electronic e : room.getElectronics()) {
			roomDTO.getElectronics().add(e.getElectronicId());
		}

		return roomDTO;

	}

	/**
	 * Convert DTO to class
	 * 
	 * @param user
	 * @return
	 */
	public User user_DTO_2_C(UserDTO user) {
		User u = new User();

		u.setUserId(user.getUserId());
		u.setName(user.getName());
		u.setPassword(user.getPassword());
		u.setHouse(em.find(House.class, user.getHouse()));
		u.setUsername(user.getUsername());
		u.setUserType(user.getUserType());

		return u;
	}

	/**
	 * Convert DTO to class
	 * 
	 * @param elec
	 * @return
	 */
	public Electronic electronic_DTO_2_C(ElectronicDTO elec) {

		Electronic el = new Electronic();
		el.setElectronicId(elec.getElectronicId());
		el.setPower(elec.getPower());
		el.setRoom(em.find(Room.class, elec.getRoom()));
		el.setType(elec.getType());

		return el;
	}

	/**
	 * Convert DTO to class
	 * 
	 * @param h
	 * @return
	 */
	public House house_DTO_2_C(HouseDTO h) {
		House house = new House();

		house.setAddress(h.getAddress());
		house.setAdminPassword(h.getAdminPassword());
		house.setHouseId(h.getHouseId());
		house.setHouseName(h.getHouseName());
		if (h.getHousePicture() != null){
			house.setHousePicture(h.getHousePicture());}
		house.setLatitude(h.getLatitude());
		house.setLongitude(h.getLongitude());
		house.setPostalCode(h.getPostalCode());
		List<Room> rooms = new ArrayList<>();
		for (int roomId : h.getRooms()) {
			rooms.add(em.find(Room.class, roomId));
		}
		house.setRooms(rooms);
		List<User> users = new ArrayList<>();
		for (int userId : h.getUsers()) {
			users.add(em.find(User.class, userId));
		}
		house.setUsers(users);

		return house;

	}

	/**
	 * Convert DTO to class
	 * 
	 * @param priv
	 * @return
	 */
	public Privilege privilege_DTO_2_C(PrivilegeDTO priv) {

		Privilege p = new Privilege();

		p.setHouseLook(priv.isHouseLook());
		p.setLights(priv.isLights());
		p.setRoomEl(priv.isRoomEl());
		p.setTemp(priv.isTemp());

		return p;
	}

	/**
	 * Convert DTO to class
	 * 
	 * @param room
	 * @return
	 */
	public Room room_DTO_2_C(RoomDTO room) {
		Room r = new Room();

		r.setLight(room.getLight());
		r.setRoomId(room.getRoomId());
		r.setSize(room.getSize());
		r.setTemperature(room.getTemperature());
		r.setType(room.getType());
		r.setHouse(em.find(House.class, room.getHouse()));

		List<Electronic> elecs = new ArrayList<>();
		for (int i : room.getElectronics()) {
			elecs.add(em.find(Electronic.class, i));
		}
		r.setElectronics(elecs);

		return r;

	}

}
